package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.constant.ErrorCode;
import com.aloda.constant.ExceptionConstant;
import com.aloda.dto.question.QuestionAddDTO;
import com.aloda.dto.question.QuestionEditDTO;
import com.aloda.entity.Question;
import com.aloda.exception.BaseException;
import com.aloda.query.QuestionPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.QuestionService;
import com.aloda.vo.question.QuestionInfoVO;
import com.aloda.vo.question.QuestionPageVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    /**
     * 管理端新增题目
     * @param questionAddDTO
     * @return
     */
    @OperateLogAnno(category = "新增题目")
    @PostMapping
    public Result<Long> addQuestion(@RequestBody QuestionAddDTO questionAddDTO) {
        if (BeanUtil.isEmpty(questionAddDTO)) {
            throw new BaseException(ExceptionConstant.FORMAT_ERROR);
        }
        return Result.success(questionService.addQuestion(questionAddDTO));
    }

    /**
     * 分页查询
     * @param questionPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<QuestionPageVO>> pageQuery(@RequestBody QuestionPageDTO questionPageDTO) {
        PageResult<QuestionPageVO> pageResult = questionService.pageQuery(questionPageDTO);
        return Result.success(pageResult);
    }


    /**
     * 通过id获取题目
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<QuestionInfoVO> getQuestionById(@PathVariable Long id) {
        QuestionInfoVO questionInfoVO = BeanUtil.copyProperties(questionService.getById(id), QuestionInfoVO.class);
        return Result.success(questionInfoVO);
    }

    /**
     * 根据appId查询题目内容
     * @param id
     * @return
     */
    @GetMapping("/app/{id}")
    public Result<String> getQuestionByAppId(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        // 查询App对应的题目
        LambdaQueryWrapper<Question> questionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        questionLambdaQueryWrapper.eq(Question::getAppId, id);
        Question question = questionService.getOne(questionLambdaQueryWrapper);
        if (BeanUtil.isEmpty(question)) {
            return null;
        }
        String questionContent = question.getQuestionContent();
        return Result.success("查询成功",questionContent);
    }

    /**
     * 删除题目
     * @param ids
     * @return
     */
    @AdminAuthCheck
    @OperateLogAnno(category = "删除题目")
    @DeleteMapping
    public Result deleteQuestion(@RequestBody List<Long> ids) {
        return Result.success(questionService.removeByIds(ids));
    }

    /**
     * 编辑题目
     * @param questionEditDTO
     * @return
     */
    @OperateLogAnno(category = "编辑题目")
    @PutMapping
    public Result editQuestion(@RequestBody QuestionEditDTO questionEditDTO) {
        questionService.editQuestion(questionEditDTO);
        return Result.success();
    }

    /**
     * 导出题目信息表格
     * @param response
     * @param ids
     */
    @OperateLogAnno(category = "导出题目信息表格")
    @PostMapping("/export")
    public void exportQuestionToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        questionService.exportQuestionToExcel(response, ids);
    }
}
